﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YQBlog.Models;
using YQBlog.General;
using Common;
using res = Resource.Models.Web.Web;
using System.Globalization;

namespace YQBlog.Helpers
{
    /// <summary>
    /// 扩展方法helpers
    /// </summary>
    public static class HtmlHelpers
    {
        /// <summary>
        /// 网站配置信息
        /// </summary>
        public static GeneralConfigInfo configinfo = YQBlog.General.WebUtils.configinfo;

        /// <summary>
        /// 字符串指定长度显示
        /// </summary>
        public static string Truncate(this HtmlHelper helper, string input, int length)
        {
            if (input.Length <= length)
            { return input; }
            else
            { return input.Substring(0, length) + " ... "; }
        }

        /// <summary>
        /// 移除html标签
        /// </summary>
        public static string RemoveHtml(this HtmlHelper helper, string input)
        {
            return input != null ? Utils.RemoveHtml(input) : "";
        }

        /// <summary>
        /// 网站文章url,根据相关设置及当前环境数据确定正确的文章url
        /// </summary>
        public static string YQUrl(this HtmlHelper helper, blog_varticle varticle)
        {
            return WebUtils.GetYQUrl(varticle);
        }

        /// <summary>
        /// 网站文章url,根据相关设置及当前环境数据确定正确的文章url
        /// </summary>
        public static string YQUrl(this HtmlHelper helper, blog_article article)
        {
            return WebUtils.GetYQUrl(article);
        }

        /// <summary>
        /// 页面url
        /// </summary>
        public static string YQUrl(this HtmlHelper helper, string url, int typeid = 0)
        {
            return WebUtils.GetYQUrl(url, typeid);
        }

        /// <summary>
        /// album页面url
        /// </summary>
        public static string YQUrl(this HtmlHelper helper, AlbumModel album)
        {
            return WebUtils.GetYQUrl(album.Url, 4);
        }

        /// <summary>
        /// 网站分类url
        /// </summary>
        public static string GetCateUrl(this HtmlHelper helper, CategoryModel category)
        {
            return WebUtils.GetCateUrl(category);
        }

        /// <summary>
        /// 网站分类url
        /// </summary>
        public static string GetCateUrl(this HtmlHelper helper, string rename, string id)
        {
            return WebUtils.GetCateUrl(rename, id);
        }

        /// <summary>
        /// 用户信息
        /// </summary>
        public static MvcHtmlString GetUserInfo(this HtmlHelper helper, string users)
        {
            string re = "";
            string[] arruser = users.Split(',');
            foreach (string user in arruser)
            {
                re += "<a href=\"" + WebUtils.GetYQUrl("/u/" + user.Trim()) + "\">" + user.Trim() + "</a> ";
            }
            return MvcHtmlString.Create(re);
        }

        /// <summary>
        /// 返回“多少xxx前”时间
        /// </summary>
        public static string DateFromNow(this HtmlHelper helper, DateTime dt, int days = 10)
        {
            return WebUtils.DateFromNow(dt, days);
        }

        /// <summary>
        /// 格式化日期
        /// </summary>
        public static string GetDate(this HtmlHelper helper, DateTime dt)
        {
            if (res.Lang == "zh-cn" || res.Lang == "zh-tw")
            {
                return dt.ToString("yyyy-MM-dd");
            }
            else
            {
                DateTimeFormatInfo myDTFI = new CultureInfo(res.Lang, false).DateTimeFormat;
                string strmon = myDTFI.GetAbbreviatedMonthName(dt.Month);
                return strmon + ". " + dt.Day + ", " + dt.Year;
            }
        }

        /// <summary>
        /// 格式化时间
        /// </summary>
        public static string GetTime(this HtmlHelper helper, DateTime dt)
        {
            if (res.Lang == "zh-cn" || res.Lang == "zh-tw")
            {
                return dt.ToString("yyyy-MM-dd HH:mm");
            }
            else
            {
                DateTimeFormatInfo myDTFI = new CultureInfo(res.Lang, false).DateTimeFormat;
                string strmon = myDTFI.GetAbbreviatedMonthName(dt.Month);
                return strmon + ". " + dt.Day + ", " + dt.Year + " " + dt.ToString("HH:mm");
            }
        }
    }

}